import request from '@/config/axios'

export interface CodegenTableVO {
  id?: number
  dataSourceConfigId: number
  dataSourceName?: string
  scene: number
  tableName: string
  tableComment: string
  remark: string
  moduleName: string
  businessName: string
  className: string
  classComment: string
  author: string
  templateType: number
  frontType: number
  parentMenuId: number
  masterTableId: number
  subJoinColumnId: number
  subJoinMany: string
  treeParentColumnId: number
  treeNameColumnId: number
  createTime?: string
  updateTime?: string
}

export interface CodegenColumnVO {
  id?: number
  tableId: number
  columnName: string
  dataType: string
  columnComment: string
  nullable: boolean
  primaryKey: boolean
  ordinalPosition: number
  javaType: string
  javaField: string
  dictType: string
  example: string
  createOperation: boolean
  updateOperation: boolean
  listOperation: boolean
  listOperationCondition: string
  listOperationResult: boolean
  htmlType: string
}

export interface CodegenTableSimpleVO {
  id: number
  dataSourceConfigId: number
  tableName: string
  tableComment: string
}

export interface DatabaseTableVO {
  tableName: string
  tableComment: string
}

export interface CodegenCreateListVO {
  dataSourceConfigId: number
  tableNames: string[]
}

export type CodegenDetailVO = {
  table: CodegenTableVO | any
  columns: CodegenColumnVO[]
}

export interface CodegenUpdateVO {
  table: CodegenTableVO | any
  columns: CodegenColumnVO[]
}

export interface CodegenPreviewVO {
  filePath: string
  fileContent: string
}

export const getCodegenTablePage = (params: PageParam) => {
  return request.get({ url: '/infra/codegen/table/page', params })
}

export const getCodegenTableList = (dataSourceConfigId: number) => {
  return request.get({
    url: '/infra/codegen/table/simple-list',
    params: { dataSourceConfigId: dataSourceConfigId }
  })
}

export const getDbTableList = (params) => {
  return request.get({ url: '/infra/codegen/db/table/list', params })
}

export const createCodegenList = (data: CodegenCreateListVO) => {
  return request.post({ url: '/infra/codegen/create-list', data: data })
}

export const updateCodegenTable = (data: CodegenUpdateVO) => {
  return request.post({ url: '/infra/codegen/update', data: data })
}

export const syncCodegenTable = (tableId: number) => {
  return request.post({ url: '/infra/codegen/sync-from-db', data: { tableId: tableId } })
}

export const getCodegenDetail = (tableId: number) => {
  return request.get({ url: '/infra/codegen/get', params: { tableId: tableId } })
}

export const deleteCodegenTable = (tableId: number) => {
  return request.delete({ url: '/infra/codegen/delete', params: { tableId: tableId } })
}

export const previewCodegen = (tableId: number) => {
  return request.get({ url: '/infra/codegen/preview', params: { tableId: tableId } })
}

export const downloadCodegen = (tableId: number) => {
  return request.download({ url: '/infra/codegen/download', params: { tableId: tableId } })
}
